home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / dostech.arc / CHAP7 < prev    next >
Text File  |  1989-02-04  |  24KB  |  449 lines

  1. CHAPTER 7
  2.  
  3.  Programming Technical Reference - IBM
  4.  Copyright 1988, Dave Williams
  5.  
  6.                              DOS File Structure
  7.  
  8.  
  9. File Management Functions
  10.  
  11.  Use DOS function calls to create, open, close, read, write, rename, find, and 
  12. erase files. There are two sets of function calls that DOS provides for support
  13. of file management. They are:
  14.  
  15. * File Control Block function calls   (0Fh-24h)
  16. * Handle function calls               (39h-62h)
  17.  
  18.  Handle function calls are easier to use and are more powerful than FCB calls.
  19. Microsoft recommends that the handle function calls be used when writing new
  20. programs. DOS 3.0 up have been curtailing use of FCB function calls; it is
  21. possible that future versions of DOS may not support FCB function calls.
  22.  The following table compares the use of FCB calls to Handle function calls:
  23.  
  24.                 FCB Calls                       Handle Calls
  25.  
  26.         Access files in current         Access files in ANY directory
  27.         directory only.
  28.  
  29.         Requires the application        Does not require use of an FCB.
  30.         program to maintain a file      Requires a string with the drive,
  31.         control block to open,          path, and filename to open, create,
  32.         create, rename or delete        rename, or delete a file. For file
  33.         a file. For I/O requests,       I/O requests, the application program
  34.         the application program         must maintain a 16 bit file handle
  35.         also needs an FCB               that is supplied by DOS.
  36.  
  37.  The only reason an application should use FCB function calls is to maintain
  38. the ability to run under DOS 1.x. To to this, the program may use only function
  39. calls 00h-2Eh.
  40.  
  41.  
  42. FCB FUNCTION CALLS
  43.  
  44.  FCB function calls require the use of one File Control Block per open file, 
  45. which is maintained by the application program and DOS. The application program
  46. supplies a pointer to the FCB and fills in ther appropriate fields required by 
  47. the specific function call. An FCB function call can perform file management on
  48. any valid drive, but only in the current logged directory. By using the current
  49. block, current record, and record length fields of the FCB, you can perform 
  50. sequential I/O by using the sequential read or write function calls. Random I/O
  51. can be performed by filling in the random record and record length fields. 
  52.  
  53.  Several possible uses of FCB type calls are considered programming errors and 
  54. should not be done under any circumstances to avoid problems with file sharing
  55. and compatibility with later versions of DOS.
  56.  Some errors are:
  57. 1) If program uses the same FCB structure to access more than one open file. By
  58.    opening a file using an FCB, doing I/O, and then replacing the filename field
  59.    in the file control block with a new filename, a program can open a second
  60.    file using the same FCB. This is invalid because DOS writes control info-
  61.    rmation about the file into the reserved fields of the FCB. If the program
  62.    then replaces the filename field with the original filename and then tries to
  63.    perform I/O on this file, DOS may become confused because the control info-
  64.    rmation has been changed. An FCB should never be used to open a second file
  65.    without closing the one that is currently open. If more than one File Control
  66.    Block is to be open concurrently, separate FCBs should be used.
  67.  
  68. 2) A program should never try to use the reserved fields in the FCB, as the
  69.    function of the fields changes with different versions of DOS.
  70.  
  71. 3) A delete or a rename on a file that is currently open is considered an error
  72.    and should not be attempted by an application program.
  73.  
  74.  It is also good programming practice to close all files when I/O is done. This
  75. avoids potential file sharing problems that require a limit on the number of
  76. files concurrently open using FCB function calls.
  77.  
  78.  
  79.  
  80. HANDLE FUNCTION CALLS
  81.  
  82.  The recommended method of file management is by using the extended "handle" 
  83. set of function calls. These calls are not restricted to the current directory.
  84. Also, the handle calls allow the application program to define the type of 
  85. access that other processes can have concurrently with the same file if the file
  86. is being shared.
  87.  
  88.  To create or open a file, the application supplies a pointer to an ASCIIZ 
  89. string giving the name and location of the file. The ASCIIZ string contains an 
  90. optional drive letter, optional path, mandatory file specification, and a 
  91. terminal byte of 00h. The following is an example of an ASCIIZ string:
  92.  
  93.                   format [drive][path] filename.ext,0
  94.  
  95.                       DB "A:\path\filename.ext",0
  96.  
  97.  If the file is being created, the application program also supplies the 
  98. attribute of the file. This is a set of values that defines the file read 
  99. only, hidden, system, directory, or volume label.
  100.  
  101.  If the file is being opened, the program can define the sharing and access 
  102. modes that the file is opened in. The access mode informs DOS what operations 
  103. your program will perform on this file (read-only, write-only, or read/write) 
  104. The sharing mode controls the type of operations other processes may perform 
  105. concurrently on the file. A program can also control if a child process inherits
  106. the open files of the parent. The sharing mode has meaning only if file sharing
  107. is loaded when the file is opened.
  108.  
  109.  To rename or delete a file, the appplication program simply needs to provide 
  110. a pointer to the ASCIIZ string containing the name and location of the file 
  111. and another string with the neew name if the file is being renamed.
  112.  
  113.  The open or create function calls return a 16-bit value referred to as the 
  114. file handle. To do any I/O to a file, the program uses the handle to reference
  115. the file. Once a file is opened, a program no longer needs to maintain the 
  116. ASCIIZ string pointing to the file, nor is there any need to stay in the same 
  117. directory. DOS keeps track of the location of the file regardless of what 
  118. directory is current.
  119.  
  120.  Sequential I/O can be performed using the handle read (3Fh) or write (40h) 
  121. function calls. The offset in the file that IO is performed to is automatically
  122. moved to the end of what was just read or written. If random I/O is desired, the
  123. LSEEK (42h) function call can be used to set the offset into the file where I/O 
  124. is to be performed.
  125.  
  126.  
  127. SPECIAL FILE HANDLES
  128.  
  129.  DOS reserves five special file handles for use by itself and applications 
  130. programs. They are:
  131.  
  132.               0000h   STDIN   Standard Input Device
  133.               0001h   STDOUT  Standard Output Device
  134.               0002h   STDERR  Standard Error Output Device
  135.               0003h   STDAUX  Standard Auxiliary Device
  136.               0004h   STDPRN  Standard Printer Device
  137.  
  138.  These handles are predefined by DOS and can be used by an application program.
  139. They do not need to be opened by a program, although a program can close these 
  140. handles. STDIN should be treated as a read-only file, and STDOUT and STDERR 
  141. should be treated as write-only files. STDIN and STDOUT can be redirected. All 
  142. handles inherited by a process can be redirected, but not at the command line.
  143.  
  144.  These handles are very useful for doing I/O to and from the console device. 
  145. For example,  you could read input from the keyboard using the read (3Fh) 
  146. function call and file handle 0000h (STDIN), and write output to the console 
  147. screen with the write function call (40h) and file handle 0001h (STDOUT). If 
  148. you wanted an output that could not be redirected, you could output it using 
  149. file handle 0002h (STDERR). This is very useful for error messages that must 
  150. be seen by a user.
  151.  
  152.  File handles 0003h (STDAUX) and 0004h (STDPRN) can be both read from and 
  153. written to. STDAUX is typically a serial device and STDPRN is usually a parallel
  154. device.
  155.  
  156.  
  157. ASCII and BINARY MODE
  158.  
  159.  I/O to files is done in binary mode. This means that the data is read or 
  160. written without modification. However, DOS can also read or write to devices in
  161. ASCII mode. In ASCII mode, DOS does some string processing and modification to 
  162. the characters read and written. The predefined handles are in ASCII mode when 
  163. initialized by DOS. All other file handles that don't refer to devices are in 
  164. binary mode. A program, can use the IOCTL (44h) function call to set the mode 
  165. that I/O is to a device. The predefined file handles are all devices, so the 
  166. mode can be changed from ASCII to binary via IOCTL. Regular file handles that 
  167. are not devices are always in binary mode and cannot be changed to ASCII mode.
  168.  
  169.  The ASCII/BINARY bit was called "raw" in DOS 2.x, but it is called ASCII/BINARY
  170. in DOS 3.x.
  171.  
  172.  The predefined file handles STDIN (0000h) and STDOUT (0001h) and STDERR 
  173. (0002h) are all duplicate handles. If the IOCTL function call is used to change
  174. the mode of any of these three handles, the mode of all three handles is 
  175. changed. For example, if IOCTL was used to change STDOUT to binary mode, then 
  176. STDIN and STDERR would also be changed to binary mode.
  177.  
  178.  
  179.  
  180. FILE I/O IN BINARY (RAW) MODE
  181.  
  182. The following is true when a file is read in binary mode:
  183.  
  184. 1)  The characters ^S (scroll lock), ^P (print screen), ^C (control break) are 
  185.     not checked for during the read. Therefore, no printer echo occurs if ^S or
  186.     ^P are read.
  187. 2)  There is no echo to STDOUT (0001h).
  188. 3)  Read the number of specified bytes and returns immediately when the last 
  189.     byte is received or the end of file reached.
  190. 4)  Allows no editing of the ine input using the function keys if the input is 
  191.     from STDIN (0000h).
  192.  
  193.  
  194. The following is true when a file is written to in binary mode:
  195.  
  196. 1)  The characters ^S (scroll lock), ^P (print screen), ^C (control break) are 
  197.     not checked for during the write. Therefore, no printer echo occurs.
  198. 2)  There is no echo to STDOUT (0001h).
  199. 3)  The exact number of bytes specified are written.
  200. 4)  Does not caret (^) control characters. For example, ctrl-D is sent out as 
  201.     byte 04h instead of the two bytes ^ and D.
  202. 5)  Does not expand tabs into spaces. 
  203.  
  204.  
  205. FILE I/O IN ASCII (COOKED) MODE
  206.  
  207. The following is true when a file is read in ASCII mode:
  208.  
  209. 1)  Checks for the characters ^C,^S, and ^P.
  210. 2)  Returns as many characters as there are in the device input buffer, or the 
  211.     number of characters requested, whichever is less. If the number of 
  212.     characters requested was less than the number of characters in the device 
  213.     buffer, then the next read will address the remaining characters in the 
  214.     buffer.
  215. 3)  If there are no more bytes remaining in the device input buffer, read a 
  216.     line (terminated by ^M) into the buffer. This line may be edited with the 
  217.     function keys. The characters returned terminated with a sequence of 0Dh,
  218.     0Ah (^M,^J) if the number of characters requested is sufficient to include
  219.     them. For example, if 5 characters were requested, and only 3 were entered
  220.     before the carriage return (0Dh or ^M) was presented to DOS from the console
  221.     device, then the 3 characters entered and 0Dh and 0Ah would be returned. 
  222.     However, if 5 characters were requested and 7 were entered before the 
  223.     carriage return, only the first 5 characters would be returned. No 0Dh,0Ah 
  224.     sequence would be returned in this case. If less than the number of 
  225.     characters requested are entered when the carriage return is received, the
  226.     characters received and 0Dh,0Ah would be returned. The reason the 0Ah 
  227.     (linefeed or ^J) is added to the returned characters is to make the devices
  228.     look like text files.
  229. 4)  If a 1Ah (^Z) is found, the input is terminated at that point. No 0Dh,0Ah 
  230.     (CR,LF) sequence is added to the string.
  231. 5)  Echoing is performed.
  232. 6)  Tabs are expanded.
  233.  
  234.  
  235. The following is true when a file is written to in ASCII mode:
  236.  
  237. 1)  The characters ^S,^P,and ^C are checked for during the write operation.
  238. 2)  Expands tabs to 8-character boundaries and fills with spaces (20h).
  239. 3)  Carets control characters, for example, ^D is written as two bytes, ^ and D.
  240. 4)  Bytes are output until the number specified is output or a ^Z is 
  241.     encountered. The number actually output is returned to the user.
  242.  
  243.  
  244. NUMBER OF OPEN FILES ALLOWED
  245.  
  246.  The number of files that can be open concurrently is restricted by DOS. This 
  247. number is determined by how the file is opened or created (FCB or handle 
  248. function call) and the number specified by the FCBS and FILES commands in the 
  249. CONFIG.SYS file. The number of files allowed open by FCB function calls and the
  250. number of files that can be opened by handle type calls are independent of one 
  251. another.
  252.  
  253.  
  254. RESTRICTIONS ON FCB USAGE
  255.  
  256.  If file sharing is not loaded using the SHARE command, there are no 
  257. restrictions on the nuumber of files concurrently open using FCB function calls.
  258.  
  259.  However, when file sharing is loaded, the maximum number of FCBs open is set
  260. by the the FCBS command in the CONFIG.SYS file.
  261.  
  262.  The FCBS command has two values you can specify, 'm' and 'n'. The value for 
  263. 'm' specifies the number of files that can be opened by FCBs, and the value 'n' 
  264. specifies the number of FCBs that are protected from being closed.
  265.  
  266.  When the maximum number of FCB opens is exceeded, DOS automatically closes the
  267. least recently used file. Any attempt to access this file results in an int 24h
  268. critical error message "FCB not availible". If this occurs while an application
  269. program is running, the value specified for 'm' in the FCBS command should be
  270. increased.
  271.  
  272.  When DOS determines the least recently used file to close, it does not include
  273. the first 'n' files opened, therefore the first 'n' files are protected from 
  274. being closed.
  275.  
  276.  
  277. RESTRICTIONS ON HANDLE USAGE
  278.  
  279.  The number of files that can be open simultaneously by all processes is 
  280. determined by the FILES command in the CONFIG.SYS file. The number of files a 
  281. single process can open depends on the value specified for the FILES command. If
  282. FILES is greater than or equal to 20, a single process can open 20 files. If 
  283. FILES is less than 20, the process can open less than 20 files. This value 
  284. includes three predefined handles: STDIN, STDOUT, and STDERR. This means only
  285. 17 additional handles can be added. DOS 3.3 includes a function to use more than
  286. 20 files per application.
  287.  
  288. ALLOCATING SPACE TO A FILE
  289.  
  290.  Files are not nescessarily written sequentially on a disk. Space is allocated 
  291. as needed and the next location availible on the disk is allocated as space for
  292. the next file being written. Therefore, if considerable file generation has
  293. taken place, newly created files will not be written in sequential sectors.
  294. However, due to the mapping (chaining) of file space via the File Allocation
  295. Table (FAT) and the function calls availible, any file may be used in either a
  296. sequential or random manner.
  297.  
  298.  Space is allocated in increments called clusters. Cluster size varies 
  299. according to the media type. An application program should not concern itself 
  300. with the way that DOS allocates space to a file. The size of a cluster is only 
  301. important in that it determines the smallest amount of space that can be 
  302. allocated to a file. A disk is considered full when all clusters have been 
  303. allocated to files.
  304.  
  305.  
  306.  
  307. MSDOS / PCDOS DIFFERENCES
  308.  
  309.  There is a problem of compatibility between MS-DOS and IBM PC-DOS having to 
  310. do with FCB Open and Create. The IBM 1.0, 1.1, and 2.0 documentation of OPEN
  311. (call 0Fh) contains the following statement:
  312.  
  313.  "The current block field (FCB bytes C-D) is set to zero [when an FCB is 
  314. opened]."
  315.  
  316.  This statement is NOT true of MS-DOS 1.25 or MS-DOS 2.00. The difference is
  317. intentional, and the reason is CP/M 1.4 compatibility. Zeroing that field is 
  318. not CP/M compatible. Some CP/M programs will not run when machine translated if
  319. that field is zeroed. The reason it is zeroed in the IBM versions is that IBM 
  320. specifically requested that it be zeroed. This is the reason for the complaints
  321. from some vendors about the fact that IBM MultiPlan will not run under MS-DOS.
  322. It is probably the reason that some other IBM programs don't run under MS-DOS.
  323.  
  324. NOTE: Do what all MS/PC-DOS Systems programs do: Set every single FCB field you
  325. want to use regardless of what the documentation says is initialized.
  326.  
  327.  
  328.  
  329. .EXE FILE STRUCTURE
  330.  
  331.  The EXE files produced by the Linker program consist of two parts, control and
  332. relocation information and the load module itself.
  333.  
  334.  The control and relocation information, which is described below, is at the 
  335. beginning of the file in an area known as the header. The load module 
  336. immediately follows the header. The load module begins in the memory image of 
  337. the module contructed by the Linker.
  338.  
  339.  When you are loading a file with the name *.EXE, DOS does NOT assume that it
  340. is an EXE format file. It looks at the first two bytes for a signature telling
  341. it that it is an EXE file. If it has the proper signature, then the load 
  342. proceeds. Otherwise, it presumes the file to be a .COM format file.
  343.  
  344.  If the file has the EXE signature, then the internal consistency is checked.
  345. Pre-2.0 versions of MSDOS did not check the signature byte for EXE files.
  346.  
  347.  The .EXE format can support programs larger than 64K. It does this by 
  348. allowing separate segments to be defined for code, data, and the stack, each 
  349. of which can be up to 64K long. Programs in EXE format may contain explicit 
  350. references to segment addresses. A header in the EXE file has information for 
  351. DOS to resolve these references.
  352.  
  353.  
  354. The .EXE header is formatted as follows:
  355. ┌─────────┬───────────────────────────────────────────────────────────────────┐
  356. │ Offset  │                      C O N T E N T S                              │
  357. ├─────────┼─────┬─────────────────────────────────────────────────────────────┤
  358. │   00h   │ 4Dh │ This is the Linker's signature to mark the file as a valid  │ 
  359. ├─────────┼─────┤ .EXE file  (The ASCII letters M and Z, for Mark Zbikowski,  │
  360. │   01h   │ 5Ah │ one of the major designers of DOS at Microsoft)             │
  361. ├─────────┼─────┴─────────────────────────────────────────────────────────────┤
  362. │ 02h-03h │ Length of the image mod 512 (remainder after dividing the load    │
  363. │         │ module image by 512)                                              │
  364. ├─────────┼───────────────────────────────────────────────────────────────────┤
  365. │ 04h-05h │ Size of the file in 512 byte pages including the header.          │ 
  366. ├─────────┼───────────────────────────────────────────────────────────────────┤
  367. │ 06h-07h │ Number of relocation table items.                                 │
  368. ├─────────┼───────────────────────────────────────────────────────────────────┤
  369. │ 08h-09h │ Size of the header in 16 byte increments (paragraphs). This is    │
  370. │         │ used to locate the beginning of the load module in the file.      │
  371. ├─────────┼───────────────────────────────────────────────────────────────────┤
  372. │ 0Ah-0Bh │ Minimum number of 16 byte paragraphs required above the end of    │
  373. │         │ the loaded program.                                               │
  374. ├─────────┼───────────────────────────────────────────────────────────────────┤
  375. │ 0Ch-0Dh │ Maximum number of 16 byte paragraphs required above the end of    │
  376. │         │ the loaded program.                                               │
  377. ├─────────┼───────────────────────────────────────────────────────────────────┤
  378. │ 0Eh-0Fh │ Displacement in paragraphs of stack segment within load module.   │
  379. ├─────────┼───────────────────────────────────────────────────────────────────┤
  380. │ 10h-11h │ Offset to be in SP register when the module is given control.     │
  381. ├─────────┼───────────────────────────────────────────────────────────────────┤
  382. │ 12h-13h │ Word Checksum - negative sum of all the words in the file,        │
  383. │         │ ignoring overflow.                                                │
  384. ├─────────┼───────────────────────────────────────────────────────────────────┤
  385. │ 14h-15h │ Offset to be in the IP register when the module is given control. │
  386. ├─────────┼───────────────────────────────────────────────────────────────────┤
  387. │ 16h-17h │ Displacement in paragraphs of code segment within load module.    │
  388. ├─────────┼───────────────────────────────────────────────────────────────────┤
  389. │ 18h-19h │ Displacement in bytes of the first relocation item in the file.   │
  390. ├─────────┼───────────────────────────────────────────────────────────────────┤
  391. │ 1Ah-1Bh │ Overlay number (0 for the resident part of the program)           │
  392. └─────────┴───────────────────────────────────────────────────────────────────┘
  393.  
  394.  
  395.  
  396. THE RELOCATION TABLE
  397.  
  398.  The word at 18h locates the first entry in the relocation table. The 
  399. relocation table is made up of a variable number of relocation items. The number
  400. of items is contained at offset 06-07. The relocation item contains two fields
  401. - a 2 byte offset value, followed by a 2 byte segment value. These two fields 
  402. represent the displacement into the load module before the module is given 
  403. control. The process is called relocation and is accomplished as follows:
  404.  
  405. 1. A Program Segment Prefix is built following the resident portion of the 
  406.    program that is performing the load operation.
  407.  
  408. 2. The formatted part of the header is read into memory (its size is at 
  409.    offset 08-09)
  410.  
  411. 3. The load module size is determined by subtracting the header size from the 
  412.    file size. Offsets 04-05 and 08-09 can be used for this calculation. The 
  413.    actual size is downward adjusted based on the contents of offsets 02-03. 
  414.    Note that all files created by the Linker programs prior to version 1.10 
  415.    always placed a value of 4 at this location, regardless of the actual 
  416.    program size. Therefore, Microsoft recommends that this field be ignored if 
  417.    it contains a value of 4. Based on the setting of the high/low loader switch,
  418.    an appropriate segment is determined for loading the load module. This
  419.    segment is called the start segment.
  420.  
  421. 4. The load module is read into memory beginning at the start segment. The
  422.    relocation table is an ordered list of relocation items. The first relocation
  423.    item is the one that has the lowest offset in the file. 
  424.  
  425. 5. The relocation table items are read into a work area one or more at a time.
  426.  
  427. 6. Each relocation table item segment value is added to the start segment value.
  428.    The calculated segment, in conjunction with the relocation item offset value,
  429.    points to a word in the load module to which is added the start segment 
  430.    value. The result is placed back into the word in the load module.
  431.  
  432. 7. Once all the relocation items have been processed, the SS and SP registers 
  433.    are set from the values in the header and the start segment value is added 
  434.    to SS. The ES and DS registers are set to the segment address of the program 
  435.    segment prefix. The start segment value is added to the header CS register 
  436.    value. The result, along with the header IP value, is used to give the 
  437.    module control.
  438.  
  439.  
  440. "NEW" .EXE FORMAT (Microsoft Windows and OS/2)
  441.  
  442.  The "old" EXE format is documented here. The "new" EXE format puts more 
  443. information into the header section and is currently used in applications that 
  444. run under Microsoft Windows. The linker that creates these files comes with the 
  445. Microsoft Windows Software Development Kit and is called LINK4. If you try to 
  446. run a Windows-linked program under DOS, you will get the error message "This 
  447. program requires Microsoft Windows".
  448.  
  449.